package com.zcf.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import java.net.HttpURLConnection;
import java.net.URL;

import java.net.URLEncoder;

public class Sms {

	public static String sendMessage(String phone, String code) throws Exception {

		String HTTP_BACK_MESSAGE = "";
		String url="https://sapi.253.com/msg/HttpBatchSendSM";
		String param="account=Hledou8&pswd=z123456.&mobile="+phone+"&msg=您的注册验证码是："+code+" 请完成注册&needstatus=false";
		HTTP_BACK_MESSAGE = HTTP_POST(url,param);
		String[] str = HTTP_BACK_MESSAGE.split(",");
		if ("0".equals(str[1])) {
			return "TRUE";
		}
		return "FALSE";
	}

	public static String HTTP_POST(String URL, String Data) throws Exception {
		BufferedReader In = null;
		PrintWriter Out = null;
		HttpURLConnection HttpConn = null;
		try {
			java.net.URL url = new URL(URL);
			HttpConn = (HttpURLConnection) url.openConnection();
			HttpConn.setRequestMethod("POST");
			HttpConn.setDoInput(true);
			HttpConn.setDoOutput(true);
			HttpConn.setRequestProperty("Accept-Charset", "UTF-8");
			Out = new PrintWriter(new OutputStreamWriter(HttpConn.getOutputStream(),"UTF-8"));
			Out.println(Data);
			Out.flush();

			if (HttpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				StringBuffer content = new StringBuffer();
				String tempStr = "";
				In = new BufferedReader(new InputStreamReader(HttpConn.getInputStream(),"UTf-8"));
				while ((tempStr = In.readLine()) != null) {
					content.append(tempStr);
				}
				In.close();
				return content.toString();
			} else {
				throw new Exception("HTTP_POST_ERROR_RETURN_STATUS：" + HttpConn.getResponseCode());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			Out.close();
			HttpConn.disconnect();
		}
		return null;
	}

}
